home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / z150_src.lzh / ZOOADD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-12  |  22.6 KB  |  626 lines

  1. #ifndef LINT
  2. /* @(#) zooadd.c 1.22 87/05/30 00:04:52 */
  3. static char sccsid[]="@(#) zooadd.c 1.22 87/05/30 00:04:52";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. */
  9. #include "options.h"
  10. /* Adds files specified in parameter-list to archive zoo_path. */
  11.  
  12. #define LONGEST    20                    /* assumed length of longest filename */
  13. #include "zoomem.h"             /* to define MAXADD */
  14. #include "zoo.h"
  15. #include <stdio.h>
  16. #include "various.h"
  17. #include "parse.h"
  18. #include "debug.h"
  19.  
  20. #include "portable.h"
  21. /* for low-level I/O */
  22. #ifdef NOFCNTL
  23. #include <file.h>
  24. #else
  25. #include <fcntl.h>
  26. #endif
  27.  
  28. #ifdef FLAT
  29. #include <types.h>
  30. #include <stat.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #endif
  35.  
  36. #ifdef IGNORECASE
  37. #define    COMPARE    strcmpi
  38. #else
  39. #define    COMPARE    strcmp
  40. #endif
  41.  
  42. #include "zoofns.h"
  43. #include "errors.i"
  44. extern int break_hit;
  45. extern int quiet;
  46.  
  47. #ifdef LINT_ARGS
  48. void opts_add (char *, int *, int *, int *, int *, int *, int *,
  49.                int *, int *, int *, int *, int *);
  50. int ver_too_high (struct zoo_header *);
  51. get_comment (struct direntry *, FILE *, char *);
  52. void copyfields (struct direntry *, struct tiny_header *);
  53. void storefname (struct direntry *, char *, int);
  54. char *choosefname (struct direntry *);
  55. #else
  56. void opts_add();
  57. int ver_too_high ();
  58. get_comment ();
  59. void copyfields ();
  60. void storefname ();
  61. char *choosefname ();
  62. #endif
  63.  
  64. extern struct zoo_header zoo_header;
  65.  
  66. extern char file_leader[];
  67. extern unsigned int crccode;
  68.  
  69. void zooadd(zoo_path, argc, argv, option)
  70. char *zoo_path;      /* pathname of zoo archive to add to */
  71. int argc;            /* how many filespecs supplied */
  72. char **argv;         /* array of pointers to filespecs */
  73. char *option;        /* option string */
  74. {
  75. char *whichname;                          /* which name to show user */
  76. char *flist[MAXADD];                      /* list of ptrs to input fnames */
  77. int fptr;                                 /* will point to within flist */
  78. int zoo_han;                              /* handle for open archive */
  79. int this_han;                             /* handle of file to add */
  80. char zoo_fname[LFNAMESIZE];               /* basename of archive itself */
  81. char zoo_bak[LFNAMESIZE];                 /* name of archive's backup */
  82. char this_fname[LFNAMESIZE];              /* just filename of file to add */
  83. char latest_name[LFNAMESIZE];             /* latest name in archive */
  84. long last_old = 0L;                       /* last direntry in old chain */
  85. FILE *zoo_file;                           /* stream for open archive */
  86. char *this_path;                          /* pathname of file to add */
  87.  
  88. #ifdef NOENUM
  89. #define NEW_ZOO 1
  90. #define OLD_ZOO 2
  91. int zoo_status;
  92. #else
  93. enum {NEW_ZOO, OLD_ZOO} zoo_status;       /* newly created or not */
  94. #endif
  95.  
  96. long this_dir_offset;                     /* pointers to within archive */
  97. long save_position;                       /* pointer to within archive */
  98. long prev_pos;                            /* posn of prev file of same name */
  99. struct direntry direntry;                 /* directory entry */
  100. struct direntry dir2entry;                /* spare */
  101. struct tiny_header tiny_header;           /* for Z format archives */
  102. int status;                               /* error status */
  103. int success;                              /* successful addition of file? */
  104. int addcount = 0;                         /* number added */
  105. int update=0;                             /* add only files already in archive */
  106. int suppress=0;                           /* suppress compression */
  107. int new=0;                                /* add only files not in archive */
  108. int zootime = 0;                          /* just set archive time */
  109. int add_comment = 0;                      /* add comment */
  110. int pack = 0;                             /* pack after adding */
  111. int need_dir = 1;                         /* store directories too */
  112. int delcount = 0;                         /* count of deleted entries */
  113. int exit_status = 0;                      /* exit status to set */
  114.  
  115. unsigned int latest_date = 0;             /* to set time on archive itself */
  116. unsigned int latest_time = 0;             /* .. same */
  117. int move = 0;                             /* delete after adding to archive */
  118. int longest;                              /* length of longest pathname added */
  119. int firstfile = 1;                        /* first file being added? */
  120. int z_fmt = 0;                            /* look for Z format files? */
  121. int inargs = 0;                                    /* read filenames from stdin? */
  122.  
  123. /* on entry option points to first letter */
  124.  
  125. opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack,
  126.           &update, &add_comment, &z_fmt, &need_dir, &inargs);
  127.  
  128. /* POSSIBLE RACE CONDITION BETWEEN TESTING EXISTENCE AND CREATING FILE */
  129. if (exists (zoo_path)) {
  130.     zoo_file = fopen (zoo_path, "r+b");            /* binary open, no create */
  131.    zoo_status = OLD_ZOO;
  132. } else {
  133.    if (!zootime)
  134.         zoo_file = fopen (zoo_path, "w+b");        /* binary create/truncate */
  135.    else
  136.       zoo_file = NULL;     /* don't create if just setting time */
  137.    zoo_status = NEW_ZOO;
  138. }
  139.  
  140. if (zoo_file == NULL)
  141.    prterror ('f', could_not_open, zoo_path);
  142. zoo_han = fileno(zoo_file);            /* get file handle */
  143.  
  144. basename(zoo_path, zoo_fname);      /* get basename of archive */
  145. rootname (zoo_path, zoo_bak);       /* name without extension */
  146. strcat (zoo_bak, BACKUP_EXT);       /* name of backup of this archive */
  147.  
  148. /* 
  149. From now on, both zoo_file and zoo_han can be used (one for stream I/O and
  150. the other for low-level I/O).  But before doing any low-level I/O, or 
  151. before switching between reading and writing on the stream, a seek
  152. must be done on the stream to flush buffered data and synchronize file
  153. pointers.
  154. */
  155.  
  156. /* Now we prepare the archive for adding one or more files.  If the archive
  157. has just been created, we write the archive header */
  158.  
  159. addfname ("",0L,0,0); /* initialize table of files already in archive */
  160. if (zoo_status == NEW_ZOO) {                 /* newly-created archive */
  161.    fwr_zooh (&zoo_header, zoo_file);
  162.    /* fwrite ((char *) &zoo_header, sizeof(zoo_header), 1, zoo_file); */
  163.    fseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  164. } else {
  165.    /* read header and rewrite with updated version numbers */
  166.    rwheader (&zoo_header, zoo_file);
  167.    /* initialize latest_name to null string */
  168.     /* NOTE:  latest_name is not currently used for anything, but
  169.         may be used in the future for inserting files into the
  170.         archive in alphabetic order. */
  171.    *latest_name = '\0';
  172.  
  173.    /* Skip existing files but add them to a list.  The variable last_old 
  174.    gets the tail of the old chain of directory entries */
  175.    skip_files (zoo_file, &latest_date, &latest_time, &delcount, 
  176.                latest_name, &last_old);
  177. }
  178. /* The file pointer is now positioned correctly to add a file to archive, 
  179. unless the null directory entry is too short.  This will be fixed below. */
  180.  
  181. /* If we are just setting time, do it and run. */
  182. if (zootime) {
  183. #ifdef NIXTIME
  184.    fclose (zoo_file);
  185.    setutime (zoo_path, latest_date, latest_time);
  186. #else
  187.    settime (zoo_han, latest_date, latest_time);
  188.    fclose (zoo_file);
  189. #endif
  190.    prterror ('m', "Archive time adjusted.\n");
  191.    exit (0);
  192. }
  193.  
  194. /* make list of files, excluding archive and its backup */
  195. longest = LONGEST;
  196. if (!inargs) {
  197.    makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest);
  198.    /*                                    ^^         ^^       ^^ exclude */
  199. }
  200.  
  201. fptr = 0;    /* ready to get filename (if makelist() was called) or to
  202.                     begin adding filenames (if reading them from stdin) */
  203.  
  204. while (1) {
  205.    unsigned int this_date, this_time;
  206.    int INLIST; /* boolean */
  207.    int RECENT; /* boolean */
  208.    int danger; /* if update requested and disk copy is out of date */
  209.     if (inargs) {
  210.     again: /* loop back if filename was same as archive name or its backup */
  211.         this_path = getstdin();            /* pathname from stdin, in static area */
  212.         modpath (this_path);
  213.         /* if moving files, add to list for later deletion;  if list overflows,
  214.             terminate addition loop and give warning message */
  215.         if (move) {
  216.             if (fptr >= MAXADD-2) {
  217.                 prterror ('w', too_many_files, MAXADD-2);
  218.                 this_path = NULL;
  219.             } else if (this_path != NULL) {
  220.                 if (!COMPARE(zoo_fname,nameptr(this_path)) ||
  221.                             !COMPARE(zoo_bak,nameptr(this_path)))
  222.                     goto again;
  223.                 else
  224.                     flist[fptr++] = strdup (this_path);
  225.             }
  226.         }
  227.     } else  {
  228.         this_path = flist[fptr++];
  229.     }
  230.     /* exit the addition loop when no more pathnames are left */
  231.     if (this_path == NULL) {
  232.         /* in case stdin was being read, make sure flist is NULL-terminated */
  233.         flist[fptr] = NULL;
  234.         break;
  235.     }
  236.  
  237.    basename (this_path, this_fname);   /* get just filename for later */
  238.  
  239.    this_han = OPEN(this_path, F_READ);
  240.    if (this_han == -1) {
  241.       prterror ('e', could_not_open, this_path);
  242.       exit_status++;
  243.       continue;
  244.    }
  245.  
  246. #ifndef PORTABLE
  247.    /* Test to see if this is a Z format file.  We assume the file is Z format
  248.       if (a) tag is correct and (b) type is 1 and (c) embedded filename
  249.       is not longer than FNAMESIZE.  
  250.    */
  251.    if (z_fmt) {
  252.       read (this_han, (char *) &tiny_header, sizeof(tiny_header));
  253.       if (tiny_header.tinytag == TINYTAG && tiny_header.type == 1 &&
  254.                         strlen (tiny_header.fname) <= FNAMESIZE)
  255.           /* ok */ ;
  256.       else {
  257.          close (this_han);
  258.          prterror ('e', "File %s does not have Z format.\n", this_fname);
  259.          exit_status++;
  260.          continue;
  261.       }
  262.    }
  263. #endif
  264.  
  265.    /* get file time;  also fix name */
  266. #ifndef PORTABLE
  267.    if (z_fmt) {
  268.       direntry.date = tiny_header.date;
  269.       direntry.time = tiny_header.time;
  270.       strcpy (direntry.fname, tiny_header.fname);
  271.       direntry.dirlen = direntry.namlen = 0;
  272.    } else {
  273. #endif
  274.  
  275.       /* Get timstamp of file being added */
  276. #ifdef GETUTIME
  277.       getutime (this_path, &direntry.date, &direntry.time);
  278. #else
  279.       gettime (this_han, &direntry.date, &direntry.time);
  280. #endif
  281.  
  282. #ifdef FOLD
  283.       strlwr(this_fname);
  284. #endif
  285.       dosname (this_fname, direntry.fname);  /* MSDOS filename */
  286.  
  287.    /*
  288.    Store long filename into direntry.lfname iff it is different from MSDOS
  289.    filename.  Also store directory name if need_dir is true.  Moved out of 
  290.    zooadd() so zooadd() doesn't get too big for optimization.
  291.    */
  292.    storefname (&direntry, this_path, need_dir);
  293.  
  294. #ifndef PORTABLE
  295.    }
  296. #endif
  297.  
  298. #ifdef DEBUG
  299. printf ("zooadd:  direntry.lfname = [%s]  direntry.dirname = [%s]\n",
  300.                   direntry.lfname, direntry.dirname);
  301. #endif
  302.  
  303.    /* if update option, then we add file if it is already in the archive 
  304.       AND the archived file is older */
  305.  
  306.    /* The following logic was derived from a Karnaugh map so it may
  307.       be hard to understand.  Essentially, if U=update requested,
  308.       N=new files requested, I=file is already in archive, and
  309.       R=file being archived is more recent than file already in
  310.       archive, then the boolean equation is:
  311.  
  312.       add = U' (N' + I') + U (IR  + I'N)
  313.    */
  314.  
  315.    /* Get the filename to use for this addition.  */
  316.    whichname = choosefname(&direntry);
  317.  
  318.    /* get position in archive of any old file of same name, ignoring
  319.         any directory prefix if need_dir is not true */
  320.    prev_pos = inlist (fullpath (&direntry), &this_date, &this_time, !need_dir);
  321.  
  322.    INLIST = prev_pos > 0;  /* already in archive if positive value */
  323.    if (INLIST) {
  324.       int result;
  325.       result = cmpnum (direntry.date, direntry.time, this_date, this_time);
  326.       RECENT = result > 0;
  327.       danger = result < 0;
  328.    } else
  329.       danger = 0; /* And RECENT is undefined and should not be used */
  330.  
  331.    if (
  332.          !update && (!new || !INLIST) ||
  333.          update && (INLIST && RECENT || !INLIST && new)
  334.       )
  335.          ;  /* then continue and add file */
  336.    else {
  337.       if (update && danger)
  338.          prterror ('w', "Archived copy of %s is newer.\n", whichname);
  339.       close (this_han);
  340.       continue;   /* cycle back, skip this file */
  341.    }
  342.  
  343. #ifdef CHEKDIR
  344.    /* Don't add if this is a directory */
  345.    if (isadir (this_han)) {
  346.       close (this_han);
  347.       continue;
  348.    }
  349. #endif
  350.  
  351. #ifdef CHEKUDIR
  352.    /* Don't add if this is a directory */
  353.    if (isuadir (this_path)) {
  354.       close (this_han);
  355.       continue;
  356.    }
  357. #endif
  358.  
  359.    /* Create directory entry for new file (but don't add just yet) */
  360.    /* NOTE:  we already got file date and time above for update option */
  361.     /* add tag, type, timezone, struc, system_id, and var_dir_len */
  362.     newdir (&direntry);
  363.  
  364.    /* 
  365.    Write a null direntry entry.  Thus, if an error occurs or the program
  366.    is interrupted, the end of the archive will still be meaningful.
  367.    Special check needed for first one written.
  368.    */
  369.  
  370.    direntry.next = direntry.offset = 0L;     /* trailing null entry */
  371.    this_dir_offset = ftell (zoo_file);
  372.    if (!firstfile) {
  373.       writedir (&direntry, zoo_file);
  374.    } else {
  375.       /*
  376.       Before adding the first file to the archive, we must make sure that
  377.       the previous directory chain (if any) is properly terminated with a
  378.       null entry of the right size.  If this is a new archive, we simply
  379.       write a new null entry of the right size.  If this is an existing
  380.       archive, we must check the size of the previous trailing null entry. 
  381.       If it is too small, we will back up to the most recent real directory
  382.       entry and change its .next field to point to end of file.  
  383.       */
  384.  
  385.       if (zoo_status == NEW_ZOO) {
  386.          writedir (&direntry, zoo_file);        /* write null dir entry */
  387.       } else {
  388. #define  DIRLEN(x)   ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len))
  389.          struct direntry tmpentry;
  390.          long tmppos;
  391.          int oldlen, newlen;
  392.          tmppos = ftell(zoo_file);
  393.          frd_dir (&tmpentry, zoo_file);
  394.          oldlen = DIRLEN(tmpentry);             /* get length of direntry */
  395.          newlen = DIRLEN(direntry);             /* ditto */
  396.  
  397.          if (newlen > oldlen) {                 /* trouble */
  398.             fseek (zoo_file, last_old, 0);      /* back to previous entry */
  399.             frd_dir (&tmpentry, zoo_file);
  400.             fseek (zoo_file, 0L, 2);            /* get EOF position */
  401.             tmpentry.next = ftell(zoo_file);    /* point to EOF */
  402.             fseek (zoo_file, last_old, 0);      /* back to previous entry */
  403.             writedir (&tmpentry, zoo_file);     /* update it */
  404.             fseek (zoo_file, 0L, 2);            /* to EOF ... */
  405.             this_dir_offset = ftell (zoo_file);
  406.             writedir (&direntry, zoo_file);     /* ...write null dir entry */
  407.          } else
  408.             fseek (zoo_file, tmppos, 0);        /* long enough -- let it be */
  409.       } /* if (zoo_status == NEW_ZOO) ... */
  410.    } /* if (!firstfile) ... */
  411.  
  412.    /* Now `this_dir_offset' is where the next directory entry will go */
  413.  
  414.    /* first file added goes at EOF to avoid overwriting comments */
  415.    if (firstfile) {
  416.       fseek (zoo_file, 0L, 2);                     /* EOF */
  417.       direntry.offset = ftell (zoo_file) + SIZ_FLDR;
  418.    } else {
  419.       direntry.offset = this_dir_offset + SIZ_DIRL + 
  420.          direntry.var_dir_len + SIZ_FLDR;
  421.    }
  422.  
  423.    direntry.major_ver = MAJOR_EXT_VER;    /* minimum version number needed */
  424.    direntry.minor_ver = MINOR_EXT_VER;    /* .. to extract */
  425.    direntry.deleted = 0;               /* not deleted, naturally */
  426.    direntry.comment = 0L;              /* no comment (yet) */
  427.    direntry.cmt_size = 0;          /* .. so no size either */
  428.  
  429.    save_position = direntry.offset;          /* save position in case of error */
  430.  
  431.    fseek (zoo_file, direntry.offset - SIZ_FLDR, 0);
  432.    fwrite (file_leader, SIZ_FLDR, 1, zoo_file);
  433.    fseek (zoo_file, ftell (zoo_file), 0);    /* for low-level I/O */
  434.  
  435. #ifdef PORTABLE
  436.    prterror ('m', "%-*s -- ", longest, this_path);
  437. #else
  438.    if (z_fmt)
  439.       prterror ('m', "%-12s <== %-*s -- ", 
  440.          direntry.fname, longest, this_path);
  441.    else
  442.       prterror ('m', "%-*s -- ", longest, this_path);
  443.  
  444. #ifdef COMMENT
  445.    prterror ('m', z_fmt ? "%-12s <== %-12s -- " : "%-12s -- ",
  446.                       direntry.fname, this_fname);       
  447.    prterror ('m', z_fmt ? "%-12s <== %-12s -- " : "%-12s -- ",
  448.                       direntry.fname, this_fname);       
  449. #endif /* COMMENT */
  450. #endif /* PORTABLE */
  451.  
  452.    crccode = 0;
  453.    if (z_fmt) {
  454.       direntry.packing_method = tiny_header.packing_method;
  455.       lseek (this_han, (long) (sizeof(tiny_header) + tiny_header.cmt_size), 0);
  456.       status = getfile (this_han, zoo_han, tiny_header.size_now, 1);
  457.    } else if (suppress) {                    /* suppress compression */
  458.       direntry.packing_method = 0;           /* no compression */
  459.       status = getfile (this_han, zoo_han, -1L, 1);
  460.       /* status = copyfile (this_han, zoo_han); */
  461.    } else {
  462.       direntry.packing_method = 1;           /* compressed */
  463.       status = lzc(this_han, zoo_han);       /* add with compression */
  464.    }
  465.    if (status != 0) { /* if I */
  466.       ++exit_status;                         /* remember error */
  467.       if (status == 1)
  468.          prterror ('F', no_memory);
  469.       else if (status == 2)
  470.          prterror ('F', disk_full);
  471.       else if (status == 3)
  472.          prterror ('F', "Read error.\n");
  473.       else
  474.          prterror ('F', internal_error);
  475.       success = 0;
  476.    } else {
  477.       direntry.next  = ftell(zoo_file);
  478.       direntry.size_now = direntry.next - direntry.offset;
  479.  
  480.       /* find and store original size of file just compressed */
  481.       direntry.org_size = tell (this_han);  /* should be EOF already */
  482.  
  483.       /* If the compressed one is bigger, just copy */
  484.  
  485.       if (direntry.size_now >= direntry.org_size &&   /* if II */
  486.             direntry.packing_method > 0) {
  487.          lseek (zoo_han, save_position, 0);     /* ..restore file pointer */
  488.          trunc (zoo_han);                       /* ..truncate file */
  489.          direntry.packing_method = 0;           /* ..and just copy */
  490.          lseek (this_han, 0L, 0);               /* (but rewind first!) */
  491.          crccode = 0;                           /* re-start crc from 0 */
  492.          status = getfile (this_han, zoo_han, -1L, 1);
  493.          /* status = copyfile (this_han, zoo_han); */
  494.          if (status != 0) {  /* if III */
  495.             success = 0;
  496.             printf (disk_full);
  497.             exit_status++;
  498.          } else {
  499.             success = 1;
  500.             direntry.next  = ftell(zoo_file);
  501.             direntry.size_now = direntry.next - direntry.offset;
  502.          } /* end if III */
  503.       } else {
  504.          success = 1;
  505.       } /* end if II */
  506.  
  507.    } /* end if I */
  508.  
  509.    if (success) {                               /* file successfully added */
  510.       addcount++;                               /* how many added */
  511.       direntry.file_crc = crccode;
  512.  
  513.       /* remember most recent date and time */
  514.       if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) {
  515.             latest_date = direntry.date;
  516.             latest_time = direntry.time;
  517.       }
  518.  
  519.       /* mark any previous version of this file in archive as deleted */
  520.       dir2entry.comment = 0L;       /* for later use assigning to direntry */
  521.       dir2entry.cmt_size = 0;
  522.  
  523.       if (!z_fmt)
  524.          prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now));
  525.  
  526.       if (prev_pos > 0) {
  527.          long save_pos = ftell(zoo_file); /*DEBUG*/
  528.          ++delcount;          /* remember to pack */
  529.          prterror ('M', "replaced\n");
  530.          fseek (zoo_file, prev_pos, 0);
  531.          readdir (&dir2entry, zoo_file, 1);
  532.          dir2entry.deleted = 1;        /* mark as deleted */
  533.          fseek (zoo_file, prev_pos, 0);
  534.          writedir (&dir2entry, zoo_file);
  535.          fseek (zoo_file, save_pos, 0); /*DEBUG*/
  536.       } else prterror ('M', "added\n");
  537.  
  538.       /* Preserve any old comment if we replaced the file */
  539.       direntry.comment = dir2entry.comment;
  540.       direntry.cmt_size = dir2entry.cmt_size;
  541.  
  542. #ifndef PORTABLE
  543.       /* Copy comment if any from Z format file */
  544.       if (z_fmt && tiny_header.cmt_size != 0) {
  545.          lseek (this_han, (long) sizeof(tiny_header), 0); /* get to comment */
  546. #ifdef COMMENT
  547.          fseek (zoo_file, 0L, 2);   /* append comment to end */
  548. #endif
  549.          direntry.comment = ftell (zoo_file);
  550.          direntry.cmt_size = tiny_header.cmt_size;
  551.          /* 4th param is 0 for no CRC */
  552.          getfile (this_han, zoo_han, (long) tiny_header.cmt_size, 0);
  553.          direntry.next = ftell(zoo_file);
  554.       } 
  555. #endif
  556.  
  557.       /* if user requested comments, any previous comment in a Z format
  558.          file may now be manually overwritten */
  559.       if (add_comment && !feof (stdin)) {
  560.          show_comment (&direntry, zoo_file, 1, whichname);
  561.          get_comment (&direntry, zoo_file, this_path);
  562.          direntry.next = ftell(zoo_file);    /* update .next ptr */
  563.       } /* end if */
  564.  
  565. #ifndef PORTABLE
  566.       /* if adding Z format archive, copy relevant fields from its header */
  567.       if (z_fmt) {   /* moved out to shorten code & allow optimizer to work */
  568.          copyfields (&direntry, &tiny_header);
  569.       }
  570. #endif
  571.  
  572.       debug((printf ("zooadd:  our new .next = [%lx].\n", direntry.next)))
  573.  
  574.       {
  575.          long savepos = ftell(zoo_file);    /* save position */
  576.          fseek (zoo_file, this_dir_offset, 0);
  577.          writedir (&direntry, zoo_file);
  578.          fseek (zoo_file, savepos, 0);    /* restore position */
  579.       }
  580.  
  581.    } else {                               /* file was not properly added */
  582.       lseek (zoo_han, save_position, 0);     /* ..restore file pointer */
  583.       trunc (zoo_han);                       /* ..truncate file */
  584.    } /* end if */
  585.    close (this_han);
  586. if (!success)
  587.    break;
  588. firstfile = 0;
  589. } /* end for */
  590.  
  591. save_position = ftell (zoo_file);
  592.  
  593. /* Write a null direntry entry */
  594. fseek (zoo_file, save_position, 0);
  595. writenull (zoo_han, MAXDIRSIZE);
  596. trunc (zoo_han);    /* truncate */
  597.  
  598. #ifdef NIXTIME
  599. fclose (zoo_file);
  600. setutime (zoo_path, latest_date, latest_time);
  601. #else
  602. settime (zoo_han, latest_date, latest_time);
  603. fclose (zoo_file);
  604. #endif
  605.  
  606. if (!addcount) {                    /* no files added */
  607.    prterror ('m', "No files added.\n");
  608.    if (zoo_status == NEW_ZOO)
  609.       unlink (zoo_path);
  610. } else {
  611.    if (delcount && pack) { /* pack if user asked and found deleted entries */
  612.       prterror ('M', "-----\nPacking...");
  613.       zoopack (zoo_path, "PP");
  614.       prterror ('M', "done\n");
  615.    }
  616.  
  617.    /* If files to move & we added some and no error so far, delete originals */
  618.    if (move && !exit_status)
  619.       if (kill_files (flist, longest) != 0)
  620.          exit_status++;
  621. }
  622.  
  623. if (exit_status)
  624.    exit (1);
  625. } /* end zoo_add */
  626.